<?php

namespace app\common\model\jy;

class Order extends CUBase {
    protected $hidden = ['openid', 'profit_sharing', 'sub_mch_id', 'sname', 'spbill_create_ip', 'delete', 'route'];
    protected $privacyFields = ['commission', 'transaction_id'];

    protected $statusEnum = 'OrderStatus';

	protected static function init()
    {
        parent::init();
        
        // 注册 before_insert 事件
        self::beforeInsert(function ($data) {
            $status = loadEnum('OrderStatus');
            $data['status'] = $status->hideStatus();
            $data['delete'] = 0;
            $data['pay_type'] = loadEnum('PayType')->code2key('notify');
            $data['paytime'] = 0;
            $data['delivery_time'] = 0;
            $data['receive_time'] = 0;
            $data['route'] = '';
            if(!isset($data['spreadid'])) {
                $data['spreadid'] = 0;
            }
            return $data;
        });
    }

    //店铺数据关联
    public function product() {
        return $this->hasMany('OrderProduct', 'oid', 'id');
    }

    //地址快照数据
    public function getSnapshotAddressAttr($value, $data) {
        return loadOnly('Model')->find('jy\\SnapshotAddress', $data['said']); 
    }

    //append店铺数据
    public function getProductsAttr($value, $data) {
        $model = loadModel('jy\\OrderProduct');
        if($this->isPrivacy) {
            $model->privacy();
        }
        $model->where(['oid' => $data['id']]);
        return $model->select();
    }

    //获取分账状态文字
    public function getProfitSharingTextAttr($value, $data) {
        $options = loadEnum('ProfitSharing')->get_options();
        return isset($options[$data['profit_sharing']]) ? $options[$data['profit_sharing']] : '';
    }

    //获取分账数据
    public function getProfitSharingAttr($value, $data) {
        return loadModel('jy\\ProfitSharing')->where(['oid' => $data['id']])->find();
    }

    //获取快递单号
    public function getTrackingAttr($value, $data) {
        $row = loadModel('jy\\OrderLogistics')->where('oid', $data['id'])->find();
        return $row ? $row['tracking'] : '';
    }

    //支付时间
    protected function getPaytimeAttr($value)
    {
        if($value == 0) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }

    //发货时间
    protected function getDeliveryTimeAttr($value)
    {
        if($value == 0) {
            return '';
        }
        return date('Y-m-d H:i:s', $value);
    }

    //获取推广用户信息
    protected function getSpreadUserAttr($value, $data) {
        $spreadid = $data['spreadid'];
        if($spreadid == 0) {
            return null;
        }
        return loadModel('tyyh\\User')->where(['id' => $spreadid])->find();
    }

    protected function getOrderLogisticsAttr($value, $data) {
        $row = loadModel('jy\\OrderLogistics')->where(['oid' => $data['id']])->find();
        return $row ? $row['tracking'] : '';
    }

    //获取店铺手机号
    protected function getStorePhoneAttr($value, $data) {
        $row = loader()->loadModel('tysj\\Store')->find($data['sid']);
        return $row ? $row['phone'] : '';
    }
}
